<template>
  <div class="dialog-role">
    <el-dialog
      :visible.sync="dialogVisible"
      :title="field"
      width="800px"
      @close="handleClose"
    >
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
        class="dialog-ruleForm"
      >
        <el-form-item label="参数名称:" prop="configName">
          <el-input v-model="ruleForm.configName" clearable />
        </el-form-item>
        <el-form-item label="参数键名:" prop="configKey">
          <el-input v-model="ruleForm.configKey" clearable />
        </el-form-item>
        <el-form-item label="参数键值:" prop="configValue">
          <el-input v-model="ruleForm.configValue" clearable />
        </el-form-item>
        <el-form-item label="系统内置:">
          <el-radio-group v-model="ruleForm.configType">
            <el-radio label="Y">是</el-radio>
            <el-radio label="N">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注:">
          <el-input v-model="ruleForm.remark" type="textarea" clearable />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { configAdd, configEdit } from "@/api/systemmanagement";
export default {
  name: "ParameterDialog",
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    field: {
      type: String,
      default: "",
    },
    rowData: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      ruleForm: {
        configName: "",
        configKey: "",
        configValue: "",
        configType: "Y",
        remark: "",
      },
      rules: {
        configName: [
          { required: true, message: "请输入参数名称", trigger: "blur" },
        ],
        configKey: [
          { required: true, message: "请输入参数键名", trigger: "blur" },
        ],
        configValue: [
          { required: true, message: "请输入参数键值", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    dialogVisible: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit("close", val);
      },
    },
  },
  watch: {
    value: {
      handler(val) {
        if (val) {
          this.initData();
        }
      },
    },
  },
  mounted() {},
  methods: {
    // 提交表单时的校验
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 校验通过，执行提交逻辑
          this.handleSubmit();
        } else {
          this.$message.error("请完善表单信息");
          return false;
        }
      });
    },
    handleSubmit() {
      let data = {
        configName: this.ruleForm.configName,
        configKey: this.ruleForm.configKey,
        configValue: this.ruleForm.configValue,
        configType: this.ruleForm.configType,
        remark: this.ruleForm.remark,
      };
      if (this.field == "添加参数") {
        configAdd(data).then((res) => {
          if (res.code == 0) {
            this.$message.success("添加成功");
            this.handleClose();
            this.$emit("init");
          }
        });
      } else {
        data.configId = this.ruleForm.configId;
        configEdit(data).then((res) => {
          if (res.code == 0) {
            this.$message.success("修改成功");
            this.handleClose();
            this.$emit("init");
          }
        });
      }
    },
    handleClose() {
      this.dialogVisible = false;
      this.$emit("close");
    },
    initData() {
      if (this.field == "添加参数") {
        this.ruleForm = {
          configName: "",
          configKey: "",
          configValue: "",
          configType: "Y",
          remark: "",
        };
        console.log(
          "Set default values for new parameter, configType:",
          this.ruleForm.configType
        );
      } else {
        this.ruleForm = { ...this.rowData };
      }
    },
  },
};
</script>

<style scoped lang="scss">
.dialog-role {
  .dialog-ruleForm {
    width: 80%;
    line-height: 60px;
    margin: 0 auto;
  }
  .menu-permisssion {
    border: 1px solid #dcdfe6;
    padding: 15px;
    border-radius: 4px;
  }
}
</style>
